﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.SessionState;
using Rison.BusinessFacade.Common;
using Rison.DataAccess.Data.ZJCP;

namespace Rison.Web.ZJCP.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SaveConvenienceQa : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            if (!CurrentLoginedUser.Logined)
            {
                context.Response.Redirect("../../Login.aspx");
            }
            else
            {
                string qaUrl = context.Request.Params["qaUrl"];
                string qaQuestion = context.Request.Params["qaQuestion"];
                string qaAnswer = context.Request.Params["qaAnswer"];
                string departmentId = context.Request.Params["departmentId"];
                string qaId = context.Request.Params["qaId"];
                if (!String.IsNullOrEmpty(departmentId))
                {
                    if (String.IsNullOrEmpty(qaId))
                    {
                        insertConvenienceQa(qaUrl, qaQuestion, qaAnswer, departmentId);
                    }
                    else
                    {
                        updateConvenienceQa(qaUrl, qaQuestion, qaAnswer, qaId);
                    }
                }
                context.Response.Redirect(String.Format("../ConvenienceQaList.aspx?departmentId={0}", departmentId));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private Boolean updateConvenienceQa(string qaUrl, string qaQuestion, string qaAnswer, string qaId)
        {
            string sql = String.Format("update ConvenienceQa set qa_url = '{0}', qa_question = '{1}', qa_answer = '{2}', qa_last_save_time = getdate() where qa_id = '{3}'", qaUrl, qaQuestion, qaAnswer, qaId);
            return DbAccess.executeUpdate(sql) == 1;
        }
        private Boolean insertConvenienceQa(string qaUrl, string qaQuestion, string qaAnswer, string departmentId)
        {
            string sql = String.Format("insert ConvenienceQa values (newid(), '{0}', '{1}', '{2}', '{3}', getdate())", departmentId, qaUrl, qaQuestion, qaAnswer);
            return DbAccess.executeUpdate(sql) == 1;
        }
    }
}
